package com.ffcs.web.controllers;

import com.ffcs.web.model.dto.JsapiResponse;
import com.ffcs.web.utils.CommonConfig;
import com.ffcs.web.utils.JsSign;
import com.ffcs.web.utils.Sha1Util;
import com.ffcs.web.utils.WxJsDemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.SortedMap;
import java.util.TreeMap;

@Controller
public class WxSignController {

	//微信支付商户开通后 微信会提供appid和appsecret和商户号partner
	private static String appid = CommonConfig.getProperty("APPID");
	private static String appsecret = CommonConfig.getProperty("APPSECRET");
	private static String partner = CommonConfig.getProperty("PARTNER");
	//这个参数partnerkey是在商户后台配置的一个32位的key,微信商户平台-账户设置-安全设置-api安全
	private static String partnerkey = CommonConfig.getProperty("PARTNERKEY");
	
	/**
	 * 获取jsspi调用所需要的参数
	 * @param url
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("getSign")
	@ResponseBody
	public JsapiResponse getSign(String url) throws Exception{
		System.out.println("url="+url+"获取jsdk签名....");
		String ticket=JsSign.getJsapiTicket();
		String nonceStr=WxJsDemo.getNonceStr();
		String timeStamp=Sha1Util.getTimeStamp();
		
		SortedMap<String, String> packageParams = new TreeMap<String, String>();
		packageParams.put("url", url);
		packageParams.put("jsapi_ticket", ticket);
		packageParams.put("noncestr", nonceStr);
		packageParams.put("timestamp", timeStamp);
		String reSign=Sha1Util.createSHA1Sign(packageParams);
		
		JsapiResponse jsapiResponse=new JsapiResponse();
		jsapiResponse.setResultCode("0");
		jsapiResponse.setResultDesc("success");
		jsapiResponse.setSignature(reSign);
		jsapiResponse.setNonceStr(nonceStr);
		jsapiResponse.setTimestamp(timeStamp);
		jsapiResponse.setAppId(appid);
		return jsapiResponse;
	}
}
